package cn.wsgfz.test;

import java.util.List;

import cn.wsgfz.testsvgapp.R;
import android.content.Context;
import android.content.res.Resources;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Matrix;
import android.graphics.PixelFormat;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.util.DisplayMetrics;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AbsListView.LayoutParams;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

public class HSvgPngAdapter extends AbsAdapter<Drawable> {
	private static final int columnNum = 2; 
	private String[] titles = {"PNG", "SVG"};
	
	public HSvgPngAdapter(Context c, List<Drawable> resource) {
		super(c, resource, columnNum);
	}

	@Override
	public View getView(int index, View view, ViewGroup vGroup) {
		if(view == null) {
			 view = getView(R.layout.imager_adapter);
			 View leftView  = view.findViewById(R.id.column_left);
			 View rightView = view.findViewById(R.id.column_right); 
			 
			 Holder holder = new Holder();
			 
			 holder.leftImageHolder = new ImageHolder();
			 holder.leftImageHolder.title = (TextView) leftView.findViewById(R.id.imageTitle);
			 holder.leftImageHolder.image = (ImageView) leftView.findViewById(R.id.imageDisplay);
			 holder.leftImageHolder.size  = (TextView) leftView.findViewById(R.id.imageSize);
			 
			 holder.rightImageHolder = new ImageHolder();
			 holder.rightImageHolder.title = (TextView) rightView.findViewById(R.id.imageTitle);
			 holder.rightImageHolder.image = (ImageView) rightView.findViewById(R.id.imageDisplay);
			 holder.rightImageHolder.size  = (TextView) rightView.findViewById(R.id.imageSize);
			 
			 view.setTag(holder);
		}
		
		Holder holder = (Holder)view.getTag();
		
		int lIndex = index == 0 ? 0 : index * columnNum;
		int rIndex = lIndex + 1;
		
		int nIndex = getResource().size() / 2 - 1;
		
		setView(holder, lIndex, rIndex, nIndex);
		
		return view;
	}
	
	/**
	 * @param holder
	 * @param lIndex
	 * @param rIndex
	 * @param nIndex 
	 */
	private void setView(Holder holder, int lIndex, int rIndex, int nIndex) {
		// 证明是第一行数据
		if(lIndex == 0 && rIndex == 1) {
			holder.leftImageHolder.title.setVisibility(View.VISIBLE);
			holder.leftImageHolder.title.setText(titles[lIndex]);
			
			holder.rightImageHolder.title.setVisibility(View.VISIBLE);
			holder.rightImageHolder.title.setText(titles[rIndex]);
		}
		
		Drawable lDraw = getResource().get(lIndex);
		Drawable rDraw = getResource().get(rIndex);
		Drawable nDraw = getResource().get(nIndex);
		
		int w = lDraw.getIntrinsicWidth();
		int h = lDraw.getIntrinsicHeight();
		RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(128 * 2, h);
		if(lIndex == 0) {
			params.setMargins(0, 40, 0, 0);
		} else {
			params.setMargins(0, 0, 0, 0);
		}
		
		holder.leftImageHolder.image.setLayoutParams(params);
		holder.leftImageHolder.image.setImageDrawable(nDraw);
		holder.leftImageHolder.size.setText(lDraw.getIntrinsicWidth() + "");
		
		holder.rightImageHolder.image.setImageDrawable(rDraw);
		holder.rightImageHolder.size.setText(rDraw.getIntrinsicWidth() + "");
	}
	
	class Holder {
		ImageHolder leftImageHolder;
		ImageHolder rightImageHolder;
	}
	
	class ImageHolder {
		TextView title;
		ImageView image;
		TextView size;
	}
}
